package city.spring.modules.material.controller;

import city.spring.modules.material.entity.MaterialwarehousingMaterialExtEntity;
import city.spring.modules.material.service.MaterialwarehousingMaterialExtService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Controller：物资入库与货架关联 原始表
 *
 * @author 谢金婷
 * @date 2020-12-30 11:44:42
 */
@RestController
@RequestMapping("/materialwarehousing_material_ext")
public class MaterialwarehousingMaterialController extends ApiController {
    private final MaterialwarehousingMaterialExtService materialwarehousingMaterialExtService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<MaterialwarehousingMaterialExtEntity, ?>> entityOrderFields;

    public MaterialwarehousingMaterialController(MaterialwarehousingMaterialExtService materialwarehousingMaterialExtService) {
        this.materialwarehousingMaterialExtService = materialwarehousingMaterialExtService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(MaterialwarehousingMaterialExtEntity::getId);
        entityOrderFields.add(MaterialwarehousingMaterialExtEntity::getMaterialwarehousingId);
        entityOrderFields.add(MaterialwarehousingMaterialExtEntity::getWarehousingquantity);
        entityOrderFields.add(MaterialwarehousingMaterialExtEntity::getTotal);
    }

    /**
     * 获取全部的 <strong>物资入库与货架关联</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, MaterialwarehousingMaterialExtEntity entity) {
        LambdaQueryChainWrapper<MaterialwarehousingMaterialExtEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>物资入库与货架关联</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, MaterialwarehousingMaterialExtEntity entity) {
        LambdaQueryChainWrapper<MaterialwarehousingMaterialExtEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<MaterialwarehousingMaterialExtEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }


    /**
     * 获取一个 <strong>物资入库与货架关联</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(materialwarehousingMaterialExtService.getById(id));
    }

    /**
     * 添加一个 <strong>物资入库与货架关联</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody MaterialwarehousingMaterialExtEntity entity) {
        materialwarehousingMaterialExtService.saveMaterialwarehousingMaterialExt(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>物资入库与货架关联</strong>
     *
     * @param entity
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody MaterialwarehousingMaterialExtEntity entity) {
        entity.setId(id);
        materialwarehousingMaterialExtService.updateMaterialwarehousingMaterialExt(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>物资入库与货架关联</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        materialwarehousingMaterialExtService.deleteMaterialwarehousingMaterialExt(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>物资入库与货架关联</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        materialwarehousingMaterialExtService.deleteMaterialwarehousingMaterialExt(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<MaterialwarehousingMaterialExtEntity> buildLambdaQuery
    (MaterialwarehousingMaterialExtEntity entity) {
        LambdaQueryChainWrapper<MaterialwarehousingMaterialExtEntity> lambdaQuery = materialwarehousingMaterialExtService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), MaterialwarehousingMaterialExtEntity::getId, entity.getId());
        lambdaQuery.eq(entity.getMaterialwarehousingId() != null, MaterialwarehousingMaterialExtEntity::getMaterialwarehousingId, entity.getMaterialwarehousingId());
        lambdaQuery.eq(entity.getWarehousingquantity() != null, MaterialwarehousingMaterialExtEntity::getWarehousingquantity, entity.getWarehousingquantity());
        lambdaQuery.eq(entity.getTotal() != null, MaterialwarehousingMaterialExtEntity::getTotal, entity.getTotal());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<MaterialwarehousingMaterialExtEntity> buildQuery(MaterialwarehousingMaterialExtEntity
                                                                                  entity) {
        QueryWrapper<MaterialwarehousingMaterialExtEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "t1.id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getSname()), "t4.sname", entity.getSname());
        return wrapper;
    }

    /**
     * 根据物资id查找入库物资
     *
     * @param materialId
     * @return
     */
    @GetMapping("/getByMaterialId/{materialId}")
    public Object getByMaterialId(@PageableDefault @SortDefault.SortDefaults({@SortDefault(sort = "warehousingdate", direction = Sort.Direction.ASC)}) Pageable pageable, MaterialwarehousingMaterialExtEntity entity, @PathVariable String materialId, Date[] warehousingdate) {
        QueryWrapper<? extends MaterialwarehousingMaterialExtEntity> queryWrapper = buildQuery(entity);
        if (warehousingdate != null && warehousingdate.length == 2) {
            queryWrapper.ge("t1.warehousingdate", warehousingdate[0]);
            queryWrapper.le("t1.warehousingdate", warehousingdate[1]);
        }
        return success(materialwarehousingMaterialExtService.getByMaterialId(MyBatisUtils.toPage(pageable), queryWrapper, materialId));
    }

    @GetMapping("/getMaterialWarehouseList/{storehouseId}/{materialId}")
    public Object getMaterialWarehouseList(@PageableDefault @SortDefault.SortDefaults({@SortDefault(sort = "warehousingdate", direction = Sort.Direction.ASC)}) Pageable pageable, @PathVariable String storehouseId, @PathVariable String materialId) {
        return success(materialwarehousingMaterialExtService.getMaterialWarehouseList(MyBatisUtils.toPage(pageable), storehouseId, materialId));
    }
}
